package com.wumart.scaffold.web.common.response;






import com.wumart.scaffold.web.common.enums.CodeMsg;
import com.wumart.scaffold.web.common.exception.BaseRuntimeException;

import java.io.PrintWriter;
import java.io.Serializable;
import java.io.StringWriter;

/**
 * 同意 JSON 响应
 * @author ruhaibo
 * @date 2017-08-15 15:55.
 */
public class HttpResult<T> implements Serializable {

    private static final String DEFAULT_VERSION = "1.0";
    /**
     * 编码
     */
    private String code;
    /**
     * 结果
     */
    private String result;
    /**
     * 数据
     */
    private T data;
    /**
     * 版本号
     */
    private String ver;

    public HttpResult() {
    }

    public HttpResult(String code, String result) {
        this.code = code;
        this.result = result;
        this.ver = DEFAULT_VERSION;
    }

    public HttpResult(String code, String result, T respData) {
        this.code = code;
        this.result = result;
        this.data = respData ;
        this.ver = DEFAULT_VERSION;
    }


    public static <T>  HttpResult<T> create(CodeMsg rspCodeMsg){
        return create(rspCodeMsg.getCode(), rspCodeMsg.getMsg(),null);
    }

    public static <T>  HttpResult<T> create(String code, String result) {
        return create(code, result , null);
    }

    public static <T>  HttpResult<T> create(String code, String result, T obj) {
        return new  HttpResult<T>(code, result , obj);
    }


    public static <T>  HttpResult<T> success() {
        return create(CodeMsg.SUCC);
    }

    public static <T>  HttpResult<T> success(String msg) {
        return create(CodeMsg.SUCC.getCode(), msg);
    }

    public static <T>  HttpResult<T> success(String msg , T obj) {
        return create(CodeMsg.SUCC.getCode(), msg , obj);
    }

    public static <T>  HttpResult<T> failure() {
        return failure(CodeMsg.FAIL);
    }

    public static <T>  HttpResult<T> failure(String msg) {
        return create(CodeMsg.FAIL.getCode(), msg);
    }

    public static <T>  HttpResult<T> failure(Exception ex) {
        return failure(ex,ex.getMessage());
    }

    public static <T>  HttpResult<T> failure(Exception ex, String message) {

        if(ex == null) {
            return failure();
        }

        if(ex instanceof BaseRuntimeException){
            return failure(((BaseRuntimeException) ex).getRspCodeMsg());
        }

         HttpResult msg = failure(message);
        try {
            StringWriter sw = new StringWriter();
            ex.printStackTrace(new PrintWriter(sw));
            msg.setData(sw.toString());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return msg;
    }

    public static <T>  HttpResult<T> failure(CodeMsg rspCodeMsg) {
        return create(rspCodeMsg);
    }

    public static <T>  HttpResult<T> failure(CodeMsg rspCodeMsg, String msg) {
        return create(rspCodeMsg.getCode(),msg == null ?rspCodeMsg.getMsg():msg);
    }

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public String getResult() {
        return result;
    }

    public void setResult(String result) {
        this.result = result;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    public String getVer() {
        return ver;
    }

    public void setVer(String ver) {
        this.ver = ver;
    }
}
